Intermediate / 1 to 5 years experienced level questions
Intermediate / 1 to 5 years experienced level questions & answers
Ques 1. Explain the concept of encapsulation.
Encapsulation is the bundling of data and methods that operate on that data into a single unit known as a class. It restricts access to some of the object's components.
Ques 2. Explain polymorphism.
Polymorphism allows objects of different types to be treated as objects of a common type. It can take the form of method overloading or method overriding.
Ques 3. Explain the concept of method overloading.
Method overloading is a feature in OOP where a class can have multiple methods with the same name but with different parameter types or a different number of parameters.
Example:
class MathOperations {
add(a, b) {
return a + b;
}
add(a, b, c) {
return a + b + c;
}
}
Ques 4. What is an interface in OOP?
An interface in OOP defines a contract for classes, specifying a set of methods that a class must implement. It allows for achieving multiple inheritance in languages that don't support it directly.
Example:
interface Printable {
print();
}
Ques 5. Explain the concept of method overriding.
Method overriding occurs when a subclass provides a specific implementation for a method that is already defined in its superclass. It allows a subclass to provide a specialized version of a method.
Example:
class Animal {
speak() {
console.log('Animal speaks');
}
}
class Dog extends Animal {
speak() {
console.log('Dog barks');
}
}
Ques 6. What is a destructor?
A destructor is a method that is called when an object is about to be destroyed. In many programming languages, like C++, it is used to release resources held by the object.
Example:
class MyClass {
destructor() {
// clean up resources
}
}
Ques 7. What is a virtual function?
A virtual function is a function in a base class that is declared using the 'virtual' keyword and can be overridden by a derived class. It enables dynamic method binding and polymorphism.
Example:
class Shape {
virtual calculateArea() = 0;
}
Ques 8. What is an abstract method?
An abstract method is a method declared in an abstract class or an interface that has no implementation in the base class. Subclasses must provide an implementation for abstract methods.
Example:
abstract class Printer {
abstract print();
}
Ques 9. Explain the term 'constructor chaining'.
Constructor chaining refers to the process of calling one constructor from another in the same class or a superclass. It allows for reusing code and initializing objects more efficiently.
Example:
class Vehicle {
constructor(make) {
this.make = make;
}
}
class Car extends Vehicle {
constructor(make, model) {
super(make);
this.model = model;
}
}
Ques 10. What is the purpose of the 'final' keyword?
In OOP, the 'final' keyword can be applied to classes, methods, or variables. It indicates that a class cannot be extended, a method cannot be overridden, or a variable cannot be reassigned.
Example:
final class MyFinalClass {
// class content
}
Ques 11. Explain the concept of method hiding.
Method hiding occurs when a subclass provides a static method with the same signature as a static method in its superclass. It does not override the method but hides it.
Example:
class Parent {
static show() {
console.log('Static method in Parent');
}
}
class Child extends Parent {
static show() {
console.log('Static method in Child');
}
}
Ques 12. What is an abstract class in C#?
In C#, an abstract class is a class that cannot be instantiated and may contain abstract methods. It is used to provide a common base for related classes and enforce a certain structure on derived classes.
Example:
abstract class Shape {
public abstract void Draw();
}
Ques 13. Explain the 'is-a' and 'has-a' relationships in OOP.
'is-a' relationship represents inheritance, where a class is a specialized version of another class. 'has-a' relationship represents composition, where a class contains an instance of another class as a member.
Most helpful rated by users: